package com.xm.shop.exceptions;

import com.xm.shop.pojo.vo.RespResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
*@author:ludi
*@date:2020/8/27 19:58
*@description:全局异常处理器
*/
@ControllerAdvice
@RestControllerAdvice
public class ShopExceptionHandler {

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public RespResult handlerException(Exception e) {
//        System.out.println("Exception handlerException");
        RespResult respResult = new RespResult();
        respResult.setCode(RespResult.Code.FAILED.getValue());
        respResult.setMessage(e.getMessage());
        return respResult;
    }

    @ResponseBody
    @ExceptionHandler(ShopException.class)
    public RespResult handlerException(ShopException e) {
//        System.out.println("ShopException handlerException");
        RespResult respResult = new RespResult();
        respResult.setCode(e.getErrorCode());
        respResult.setMessage(e.getMessage());
        return respResult;
    }
}
